﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using KnowledgeCommunity.Web.Models;
using KnowledgeCommunity.Model;
using KnowledgeCommunity.Service;

namespace KnowledgeCommunity.Web.DbHelper
{
    public class TribuneHelper
    {
        CKCDB mydb = new CKCDB();
        /// <summary>
        /// FindAll()
        /// </summary>
        /// <returns></returns>
        public IEnumerable<Tribune> FindAll()
        {
            var apps = from n in mydb.Tribunes
                       orderby n.AddTime descending
                       select n;
            return apps;
        }
        /// <summary>
        /// FindById(int id)
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public Tribune FindById(int id)
        {
            var app = mydb.Tribunes.Find(id);
            return app;
        }

        public int UserTribuneCount(string user)
        {
            int count1 = (from n in mydb.Tribunes
                          where n.Sponsor == user
                          select n).Count();
            return count1;
        }

        public int AllTribuneCount()
        {
            int count1 = (from n in mydb.Tribunes
                          select n).Count();
            return count1;
        }

        /// <summary>
        /// FindByState
        /// </summary>
        /// <param name="state">审批状态</param>
        /// <returns></returns>
        public IEnumerable<Tribune> FindByState(int state)
        {
            var apps = from n in mydb.Tribunes
                       where n.State == state
                       orderby n.AddTime descending
                       select n;
            return apps;
        }

        /// <summary>
        /// FindByUserName
        /// </summary>
        /// <param name="username">用户名</param>
        /// <returns></returns>
        public IEnumerable<Tribune> FindByUserName(string username)
        {
            var apps = from n in mydb.Tribunes
                       where n.Sponsor == username
                       orderby n.AddTime descending
                       select n;
            return apps;
        }

        /// <summary>
        /// SearchByKeyAndTime
        /// </summary>
        /// <param name="key">关键字(Title,Summary)</param>
        /// <param name="stime">起始时间</param>
        /// <param name="etime">结束时间</param>
        /// <returns></returns>
        public IEnumerable<Tribune> SearchByKeyAndTime(string key, string stime, string etime)
        {
            IEnumerable<Tribune> apps = null;
            if (key == null && stime == null && etime == null)   //000
            {
                apps = FindAll();
            }
            else if (key != null && stime == null && etime == null)   //100
            {
                apps = from n in mydb.Tribunes
                       where n.Title.Contains(key) || n.Content.Contains(key)
                       orderby n.AddTime descending
                       select n;
            }
            else if (key == null && stime != null && etime == null)   //010
            {
                apps = from n in mydb.Tribunes
                       where n.AddTime >= Convert.ToDateTime(stime)
                       orderby n.AddTime descending
                       select n;
            }
            else if (key == null && stime == null && etime != null)   //001
            {
                apps = from n in mydb.Tribunes
                       where n.AddTime <= Convert.ToDateTime(etime)
                       orderby n.AddTime descending
                       select n;
            }
            else if (key != null && stime == null && etime != null)   //101
            {
                apps = from n in mydb.Tribunes
                       where (n.Title.Contains(key) || n.Content.Contains(key)) && n.AddTime <= Convert.ToDateTime(etime)
                       orderby n.AddTime descending
                       select n;
            }
            else if (key != null && stime != null && etime == null)   //110
            {
                apps = from n in mydb.Tribunes
                       where (n.Title.Contains(key) || n.Content.Contains(key)) && n.AddTime >= Convert.ToDateTime(stime)
                       orderby n.AddTime descending
                       select n;
            }
            else if (key == null && stime != null && etime != null)   //011
            {
                apps = from n in mydb.Tribunes
                       where n.AddTime >= Convert.ToDateTime(stime) && n.AddTime <= Convert.ToDateTime(etime)
                       orderby n.AddTime descending
                       select n;
            }
            else if (key != null && stime != null && etime != null)   //111
            {
                apps = from n in mydb.Tribunes
                       where (n.Title.Contains(key) || n.Content.Contains(key)) && n.AddTime >= Convert.ToDateTime(stime) && n.AddTime <= Convert.ToDateTime(etime)
                       orderby n.AddTime descending
                       select n;
            }
            return apps;
        }

        /// <summary>
        /// return 0-Fail; 1-Success
        /// </summary>
        /// <param name="model">Tribune</param>
        /// <returns></returns>
        public bool Create(Tribune model)
        {
            try
            {
                mydb.Tribunes.Add(model);
                mydb.SaveChanges();
                return true;
            }
            catch (Exception ex)
            {
                string message = "Create in table<Tribunes>" + ex.ToString();
                WriteLog.WriteSysLog(message);
                return false;
            }
        }

        /// <summary>
        /// return 0-Fail; 1-Success
        /// </summary>
        /// <param name="model">Tribune</param>
        /// <returns></returns>
        public bool Delete(int id)
        {
            var model = FindById(id);
            try
            {
                mydb.Tribunes.Remove(model);
                mydb.SaveChanges();
                return true;
            }
            catch (Exception ex)
            {
                string message = "Delete in table<Tribunes>" + ex.ToString();
                WriteLog.WriteSysLog(message);
                return false;
            }
        }
    }
}